package com.framework.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.FlowActivDao;
import com.framework.entity.flow.FlowActiv;
import com.framework.entity.sys.SysAgent;
import com.framework.exception.RRException;
import com.framework.exception.menu.RespCode;
import com.framework.page.PageUtils;
import com.framework.service.AgentService;
import com.framework.service.FlowActivService;
import com.framework.utils.DateUtils;
import com.framework.utils.string.StringUtil;
import com.framework.utils.verified.MathUtils;
import com.framework.view.SeparatMonthView;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/***
 * @author wq
 */
@Service("flowActivService")
@Transactional(rollbackFor = Exception.class)
public class FlowActivServiceImpl extends ServiceImpl<FlowActivDao, FlowActiv> implements FlowActivService {

	@Autowired
	private AgentService agentService;



	@Override
	public BigDecimal getThisMonthAmount(Long agentId) {
		return baseMapper.getThisMonthAmount(agentId);
	}

	@Override
	public List<SeparatMonthView> queryActivInfoList(String token) {
		SysAgent agent = agentService.queryAgentByToken(token);
		Long agentId = agent.getAgentId();
		Long deptId = agent.getDeptId();
		//获取半年前时间
		List<SeparatMonthView> monthList = StringUtil.getLastMonthsList(6);
		for (int i = 0; i < monthList.size(); i++) {
			String monthTime = monthList.get(i).getTime();
			//获取一个月的激活返现金额
			BigDecimal monthSum = baseMapper.activAmountMonthSum(agentId,monthTime);
			monthList.get(i).setAmount(MathUtils.getBigDecimal(monthSum));
			//获取一个月有交易的天时间
			List<Map<String,Object>> list = baseMapper.queryActivInfoList(agentId,monthTime);
			for (int j = 0; j < list.size(); j++) {
				//查询每天的交易
				Map<String,Object> map = new HashedMap();
				String dayTime = (String)list.get(j).get("dayTime");
				Object o = list.get(j).get("amount");
				BigDecimal dayAmountSum = MathUtils.getBigDecimal(o);
				map.put("dayTime",dayTime);
				map.put("dayAmountSum",dayAmountSum);
				List<Map<String,Object>> dayList = baseMapper.queryFlowActivDayList(agentId,dayTime,deptId);
				map.put("terminalList",dayList);
				list.set(j,map);
			}
			monthList.get(i).setObjList(list);
		}
		return monthList;
	}

	@Override
	public FlowActiv queryActivBySn(String sn) {
		FlowActiv f = new FlowActiv();
		f.setSn(sn);
		return baseMapper.selectOne(f);
	}

	@Override
	public PageUtils queryActivAgentList(Map<String, Object> params) {
		//防止mybatis的费控判断报错
		if("".equals(params.get("startTime"))){
			params.put("startTime",null);
		}
		if("".equals(params.get("endTime"))){
			params.put("endTime",null);
		}
		int currPage=Integer.valueOf(params.get("page").toString());
		int pageSize=Integer.valueOf(params.get("limit").toString());
		params.put("start",(currPage-1)*pageSize);
		params.put("limit",pageSize);
		List<Map<String,Object>> list = baseMapper.queryActivAgentList(params);
		int totalCount=baseMapper.queryActivAgentCount(params);
		return new PageUtils(list,totalCount,pageSize,currPage);
	}

	@Override
	public void insertFlowActiv(Map<String, Object> params, Long deptId) {
		String name=params.get("name").toString();
		String phone=params.get("phone").toString();
		Long agentId=agentService.checkParent(phone,name, deptId);
		if(agentId==null||agentId.equals(0L)){
			throw new RRException("9000","未找到用户,请核实姓名和手机号码");
		}
		FlowActiv flowActiv=new FlowActiv();
		flowActiv.setCreateTime(new Date());
		flowActiv.setSn(params.get("sn").toString());
		flowActiv.setAgentId(agentId);
		String dateStr=params.get("activDate").toString();
		flowActiv.setActivDate(DateUtils.stringToDate(dateStr,"yyyy-MM-dd"));
		flowActiv.setAmount(new BigDecimal(params.get("amount").toString()));
		flowActiv.setActivId(String.valueOf(System.currentTimeMillis()));
		this.baseMapper.insertFlowActiv(flowActiv);
	}

	@Override
	public void deleteFlowActivById(String activId) {
		this.baseMapper.deleteFlowActivById(activId);
	}

	@Override
	public Map<String,Object> queryActivAgentById(String activId) {
		return this.baseMapper.queryActivAgentById(activId);
	}


	@Override
	public List<Map<String, Object>> queryActivByAgentId(Map<String, Object> params) {
		return baseMapper.queryActivByAgentId(params);
	}
}
